package com.star.security;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.star.result.API;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 未授权统一处理
 *
 * @author tanyapeng
 * @date 2021/3/7
 */
public class UnauthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(), API.error(HttpStatus.UNAUTHORIZED.value(),"(未授权) 请求要求身份验证!"));
    }
}
